package image

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"mime/multipart"
	"net/http"
	"path/filepath"
	"sola/common"
	"sola/utils"
	"strconv"
	"time"
)

type Image struct {
	ImageId     int64     `json:"image_id" db:"image_id"`
	ImageName   string    `json:"image_name" db:"Image_name"`
	Description string    `json:"description" db:"description"`
	BaseUrl     string    `json:"base_url" db:"base_url"`
	BasePath    string    `json:"base_path" db:"base_path"`
	UserId      int64     `json:"user_id" db:"user_id"`
	Status      int32     `json:"status" db:"status"`
	Deleted     int32     `json:"deleted" db:"deleted"`
	CreateTime  time.Time `json:"create_time" db:"create_time"`
	UpdateTime  time.Time `json:"update_time" db:"update_time"`
	DeletedAt   time.Time `json:"deleted_at" db:"deleted_at"`
}

type BindFile struct {
	//Name  string                `form:"name" binding:"required"`
	//Email string                `form:"email" binding:"required"`
	File *multipart.FileHeader `form:"file" binding:"required"`
}

func uploadFile(c *gin.Context) (statusCode int, message string) {
	var bindFile BindFile

	// Bind file
	if err := c.ShouldBind(&bindFile); err != nil {
		statusCode, message = http.StatusBadRequest, fmt.Sprintf("err: %s", err.Error())
		return
	}

	// Save uploaded file
	file := bindFile.File
	dst := filepath.Base(file.Filename)
	basfile, _ := filepath.Abs(common.BaseUploadFilePath)
	file_id, _ := utils.GetId()
	file_id_str := strconv.Itoa(int(file_id))
	basfile = filepath.Join(basfile, file_id_str)
	fmt.Println("basfile:", basfile)
	fmt.Println(dst)
	if err := c.SaveUploadedFile(file, basfile); err != nil {
		statusCode, message = http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error())
		return
	}
	//statusCode, message =http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, bindFile.Name, bindFile.Email)
	//statusCode, message = http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields.", file.Filename)
	file_info := make(map[string]string)
	//file_info["file"] = "http://49.233.81.99:8000/images/" + file_id_str
	file_info["file"] = common.ImageURL + file_id_str
	utils.ResponseSuccess(c, file_info)
	return
}

func listImages(c *gin.Context) (statusCode int, message string) {
	statusCode, message = http.StatusOK,
		fmt.Sprintf("File List successfully.")
	return
}

func getImage(c *gin.Context) (statusCode int, message string) {
	statusCode, message = http.StatusOK,
		fmt.Sprintf("File Get successfully.")
	return
}
